package com.mogist.hqc.view;

import android.content.Context;
import android.support.design.widget.TabLayout;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;

import talex.zsw.basecore.util.LogTool;

/**
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
public class ScrollViewTheAnchor
	implements TabLayout.OnTabSelectedListener, ScrollViewListener, View.OnTouchListener
{

	/**
	 * 内部的tab
	 */
	private TabLayout mInside_tab;
	/**
	 * 外部的tab
	 */
	private TabLayout mOutside_tab;

	private ObservableScrollView mScrollView;
	/**
	 * 锚点集合
	 */
	private ArrayList<TextView> mAimingPointList;

	private Context mContext;
	/**
	 * ScrollView 的滑动状态
	 */
	private int state = MotionEvent.ACTION_UP;
	/**
	 * 覆盖的高度
	 */
	private float mCoverHeight;

	/**
	 * @param inside_tab      内部Tab
	 * @param outside_tab     外部Tab
	 * @param scrollView      scrollView
	 * @param aimingPointList 定位的锚点集合
	 * @param coverHeight     覆盖在scrollView上面的 布局总高度dp值
	 */
	public ScrollViewTheAnchor(TabLayout inside_tab, TabLayout outside_tab, ObservableScrollView scrollView, ArrayList<TextView> aimingPointList, Context context, float coverHeight)
	{
		this.mInside_tab = inside_tab;
		this.mOutside_tab = outside_tab;
		this.mScrollView = scrollView;
		this.mAimingPointList = aimingPointList;
		this.mContext = context;
		this.mCoverHeight = dip2px(mContext, coverHeight);
		mOutside_tab.setOnTabSelectedListener(this);
		mInside_tab.setOnTabSelectedListener(this);
		mScrollView.setScrollViewListener(this);
		mScrollView.setOnTouchListener(this);
	}

	@Override public void onTabSelected(TabLayout.Tab tab)
	{
		Log.i("xionghui", "onTabSelected"+tab.getPosition());
		int position = tab.getPosition();
		mOutside_tab.getTabAt(position).select();
		mInside_tab.getTabAt(position).select();
		int scorlly = mScrollView.getScrollY();
		if(scorlly >= mInside_tab.getTop())
		{
			mOutside_tab.setVisibility(View.VISIBLE);
		}
		else
		{
			mOutside_tab.setVisibility(View.GONE);
		}
		if(state == MotionEvent.ACTION_UP)
		{
			mScrollView.smoothScrollTo(0, mAimingPointList.get(position).getTop()-
				dip2px(mContext, 50));
		}
	}

	@Override public void onTabUnselected(TabLayout.Tab tab)
	{
		Log.i("xionghui", "onTabUnselected"+tab.getPosition());
	}

	@Override public void onTabReselected(TabLayout.Tab tab)
	{
		Log.i("xionghui", "onTabReselected"+tab.getPosition());
	}


	/**
	 * 将dip或dp值转换为px值，保证尺寸大小不变
	 *
	 * @param context
	 * @param dipValue
	 * @return
	 */
	public static int dip2px(Context context, float dipValue)
	{
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue*scale+0.5f);
	}

	/**
	 * 当前的触摸状态
	 */
	@Override public boolean onTouch(View view, MotionEvent motionEvent)
	{
		state = motionEvent.getAction();
		return false;
	}

	@Override
	public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy)
	{
		/**已经滑动的距离*/
		int scorlly = mScrollView.getScrollY();
		/**导航控制*/
		if(scorlly >= mInside_tab.getTop())
		{
			mOutside_tab.setVisibility(View.VISIBLE);
		}
		else
		{
			mOutside_tab.setVisibility(View.GONE);
		}
		/**遍历锚点 从最下面瞄点开始遍历， 如果 已经滑动的Y距离+tablayout高度 大于 锚点的top  选中，选中该tab */
		LogTool.ni("============mAimingPointList.size() = "+mAimingPointList.size());
		for(int j = 0; j < mAimingPointList.size(); j++)
		{
			int index = mAimingPointList.size()-j-1;
			int top = mAimingPointList.get(index).getTop();
			LogTool.ni("j = "+j+"   index = "+index+"    top = "+top);
			if(scorlly+mCoverHeight >= top && top != 0)
			{
				mInside_tab.getTabAt(index).select();
				mOutside_tab.getTabAt(index).select();
				return;
			}
		}
		LogTool.ni("==========================");
	}
}
